﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using cndp.Model;

namespace cndp.View
{
    public partial class FormAddJudgeToEvent : Form
    {
        private int _rowOfRegisteredJudge = -1;
        private int _rowOfSignedJudge = -1;
        public string EventBracket;
        public bool EventCoupleBool;
        public string EventFinishTime;
        public string EventGender;
        public int EventId;
        public double EventJumpHeight;
        public int EventRounds;
        public string EventStartTime;
        public BindingList<Judge> JudgeRegister = new BindingList<Judge>();
        public BindingList<Judge> SignedToEvent = new BindingList<Judge>();

        public FormAddJudgeToEvent()
        {
            InitializeComponent();
        }

        private void FormAddJudgeToEvent_Load(object sender, EventArgs e)
        {
            KeyPreview = true;
            dataGridViewEventInformation[0, 0].Value = EventId;
            dataGridViewEventInformation[1, 0].Value = EventCoupleBool;
            dataGridViewEventInformation[2, 0].Value = EventGender;
            dataGridViewEventInformation[3, 0].Value = EventBracket;
            dataGridViewEventInformation[4, 0].Value = EventJumpHeight;
            dataGridViewEventInformation[5, 0].Value = EventRounds;
            dataGridViewEventInformation[6, 0].Value = EventStartTime;
            dataGridViewEventInformation[7, 0].Value = EventFinishTime;
            dataGridViewRegisteredToContest.DataSource = JudgeRegister;
            dataGridViewSignedToEvent.DataSource = SignedToEvent;
            label1.Visible = false;
            label2.Visible = false;
            label3.Visible = false;
            label4.Visible = false;
            label5.Visible = false;


            if (dataGridViewRegisteredToContest.Rows.Count != 0)
            {
                _rowOfRegisteredJudge = 0;
                dataGridViewRegisteredToContest.Rows[_rowOfRegisteredJudge].Selected = true;
            }

            if (dataGridViewSignedToEvent.Rows.Count != 0)
            {
                _rowOfSignedJudge = 0;
                dataGridViewSignedToEvent.Rows[_rowOfSignedJudge].Selected = true;
            }
        }

        private void buttonAddJudgeToEvent_Click(object sender, EventArgs e)
        {
            if (dataGridViewRegisteredToContest.RowCount != 0)
            {
                if (_rowOfRegisteredJudge != -1)
                {
                    var diverFound =
                        SignedToEvent.SingleOrDefault(
                            d => d.PersonalCodeNumber == JudgeRegister[_rowOfRegisteredJudge].PersonalCodeNumber);
                    if (diverFound == null)
                    {
                        SignedToEvent.Add(JudgeRegister[_rowOfRegisteredJudge]);
                    }
                }
            }
            dataGridViewSignedToEvent.Refresh();
        }

        private void buttonRemoveJudgeFromEvent_Click(object sender, EventArgs e)
        {
            if (dataGridViewSignedToEvent.RowCount != 0)
            {
                if (_rowOfSignedJudge != -1)
                {
                    dataGridViewSignedToEvent.Rows.RemoveAt(_rowOfSignedJudge);
                }
            }
            if (_rowOfSignedJudge == (dataGridViewSignedToEvent.Rows.Count))
            {
                _rowOfSignedJudge--;
            }
        }

        private void dataGridViewRegisteredToContest_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            _rowOfRegisteredJudge = e.RowIndex;
        }

        private void dataGridViewSignedToEvent_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            _rowOfSignedJudge = e.RowIndex;
        }

        private void buttonDone_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FormAddJudgeToEvent_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                Close();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Tab)
            {
                label1.Visible = !label1.Visible;
                label2.Visible = !label2.Visible;
                label3.Visible = !label3.Visible;
                label4.Visible = !label4.Visible;
                label5.Visible = !label5.Visible;
            }
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Down)
            {
                if (_rowOfRegisteredJudge != (dataGridViewRegisteredToContest.Rows.Count - 1))
                {
                    _rowOfRegisteredJudge++;
                    foreach (DataGridViewRow row in dataGridViewRegisteredToContest.Rows)
                    {
                        row.Selected = false;
                    }
                    dataGridViewRegisteredToContest_CellClick(dataGridViewRegisteredToContest,
                        new DataGridViewCellEventArgs(0, _rowOfRegisteredJudge));
                    dataGridViewRegisteredToContest.Rows[_rowOfRegisteredJudge].Selected = true;
                }
            }
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Up)
            {
                if (_rowOfRegisteredJudge != 0)
                {
                    _rowOfRegisteredJudge--;
                    foreach (DataGridViewRow row in dataGridViewRegisteredToContest.Rows)
                    {
                        row.Selected = false;
                    }
                    dataGridViewRegisteredToContest_CellClick(dataGridViewRegisteredToContest,
                        new DataGridViewCellEventArgs(0, _rowOfRegisteredJudge));
                    dataGridViewRegisteredToContest.Rows[_rowOfRegisteredJudge].Selected = true;
                }
            }
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Left)
            {
                if (_rowOfSignedJudge != 0)
                {
                    _rowOfSignedJudge--;
                    foreach (DataGridViewRow row in dataGridViewSignedToEvent.Rows)
                    {
                        row.Selected = false;
                    }
                    dataGridViewSignedToEvent_CellClick(dataGridViewSignedToEvent,
                        new DataGridViewCellEventArgs(0, _rowOfSignedJudge));

                    dataGridViewSignedToEvent.Rows[_rowOfSignedJudge].Selected = true;
                }
            }
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Right)
            {
                if (_rowOfSignedJudge != (dataGridViewSignedToEvent.Rows.Count - 1))
                {
                    _rowOfSignedJudge++;


                    foreach (DataGridViewRow row in dataGridViewSignedToEvent.Rows)
                    {
                        row.Selected = false;
                    }
                    dataGridViewSignedToEvent_CellClick(dataGridViewSignedToEvent,
                        new DataGridViewCellEventArgs(0, _rowOfSignedJudge));

                    dataGridViewSignedToEvent.Rows[_rowOfSignedJudge].Selected = true;
                }
            }
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D1)
                buttonAddJudgeToEvent.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D2)
                buttonRemoveJudgeFromEvent.PerformClick();
            if ((_rowOfSignedJudge != -1) && (dataGridViewSignedToEvent.Rows.Count != 0))
            {
                dataGridViewSignedToEvent.Rows[_rowOfSignedJudge].Selected = true;
            }
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D3)
                buttonDone.PerformClick();
        }
    }
}